package com.room.controller.classes;

import com.room.pojo.Classes;
import com.room.pojo.DormResult;
import com.room.pojo.Student;
import com.room.pojo.Teachaer;
import com.room.service.ClassesService;
import com.room.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/6
 * @描述
 */
@RestController
@RequestMapping("/class/manage")
public class ClassesController {
    @Autowired
    ClassesService classesService;

    @Autowired
    TeacherService teacherService;

    @RequestMapping("/getinitData")
    public List<Classes> getAllClasses(){
        return classesService.getAllClasses();
    }

    /**
     * 获得学生的分页数据
     * @param page
     * @param size
     * @return返回学生的分页数据
     */
    @RequestMapping(value = "/class",method = RequestMethod.GET)
    public Map<String,Object> getClassesByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){

        Map<String,Object> map = new HashMap<String,Object>();
        List<Classes> classes = classesService.getClassesByPage(page, size);
        long total = classesService.getTotal();
        map.put("classes",classes);
        map.put("total",total);
        return map;
    }


    /**
     * 删除指定id的班级
     * @param ids
     * @return
     */
    @RequestMapping(value = "/class/{ids}",method = RequestMethod.DELETE)
    public DormResult deleteById(@PathVariable String ids){
        if(classesService.deleteClassesById(ids)){
            return DormResult.ok("删除成功");
        }
        return DormResult.error("删除失败");
    }


    /**
     * 修改班级信息
     * @param classes
     * @return
     */
    @RequestMapping(value = "/class",method = RequestMethod.PUT)
   public DormResult updateClass(Classes classes){
        if(classesService.updateClass(classes)==1){
            return DormResult.ok("修改成功");
        }
        return DormResult.error("修改失败");
    }

    @RequestMapping(value = "/class",method = RequestMethod.POST)
    public DormResult addClasses(Classes classes){
        String maxNo = null;
        if(classesService.getMaxId()==null){
            maxNo = "1";
        }else{
            maxNo = classesService.getMaxId();
            int a = Integer.parseInt(maxNo);
           a++;
            maxNo = String.valueOf(a);
        }
        classes.setClassId(maxNo);
        String phone = null;
        if(teacherService.getPhoneFormName(classes.getCoach())!=null){
            phone = teacherService.getPhoneFormName(classes.getCoach());
        }
        classes.setCoachCall(phone);
        if(classesService.insertClases(classes)==1){
            return DormResult.ok("添加成功");
        }
        return DormResult.error("添加失败");
    }


}
